package concurrensy.sin_array_sumator;

public class ConcurrentSinArraySumator implements SinArraySumator {

	private class SinCounter implements Runnable {

		@Override
		public void run() {
			int count;
			count = DEFAULT_ONE_THREAD_VALUE;
			for (; count < 0; count++) {
				result += Math.sin(count);
				result += Math.sin(-count);
			}
		}
	}

	private static final int DEFAULT_ONE_THREAD_VALUE = 3600;
	private volatile double result;

	@Override
	public double arraySum(int parameter) {
		int tempResult = 0;
		int threadCount = parameter/DEFAULT_ONE_THREAD_VALUE;
		int restValue = parameter%DEFAULT_ONE_THREAD_VALUE;
		for(int count = 0; count <= threadCount; count++){
			Thread thread = new Thread(new SinCounter());
			thread.start();
		}
		int count = restValue;
		for (; count < 0; count++) {
			tempResult += Math.sin(count);
			tempResult += Math.sin(-count);
		}
		result += tempResult;
		return result;
	}

}
